package com.weiaibenpao.demo.chislim.ui.marathon;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.bean.Li_UserBeanResult;
import com.weiaibenpao.demo.chislim.bean.UserBean;
import com.weiaibenpao.demo.chislim.customs.CustomDatePicker;
import com.weiaibenpao.demo.chislim.model.MyModel;
import com.weiaibenpao.demo.chislim.ui.MainActivity;
import com.weiaibenpao.demo.chislim.util.ScaleView;
import com.weiaibenpao.demo.chislim.util.WriteReadSharedPrefs;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CompleteInfoActivity extends AppCompatActivity  {

    @BindView(R.id.text_index)
    TextView textIndex;
    List<View> list;
    @BindView(R.id.com_radio_btn1)
    RadioButton comRadioBtn1;
    @BindView(R.id.com_radio_btn2)
    RadioButton comRadioBtn2;
    @BindView(R.id.com_group)
    RadioGroup comGroup;
    @BindView(R.id.tv_com_height)
    TextView tvComHeight;

    @BindView(R.id.update_datePicker)
    CustomDatePicker updateDatePicker;
    @BindView(R.id.btn_next)
    Button btnNext;
    @BindView(R.id.index_1)
    View view1;
    @BindView(R.id.index_2)
    View view2;
    @BindView(R.id.index_3)
    View view3;
    @BindView(R.id.tv_style)
    TextView tvStyle;
    @BindView(R.id.date_layout)
    View dateLayout;

    @BindView(R.id.tv_com_weight)
    TextView tvComWeight;
    @BindView(R.id.btn_back)
    Button btnBack;
    @BindView(R.id.ruler1)
    ScaleView ruler1;
    @BindView(R.id.ruler2)
    ScaleView ruler2;

    private int index = 0;

    private String sex = "男";
    private String birth = "1980-01-01";
    private float weight = 55.0f;
    private int height = 175;
    private UserBean userBean;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complete_info);
        context=this;
        //透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //透明导航栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        ButterKnife.bind(this);
        userBean = UserBean.getUserBean();
        initView();
    }

    private void initView() {
        list = new ArrayList<>();
        list.add(view1);
        list.add(view2);
        list.add(view3);
        list.add(dateLayout);
        comRadioBtn2.setChecked(true);
        /**
         * 设置选中的条目
         */

        ruler1.setOnSelectItem(new ScaleView.SelectItem() {
            @Override
            public int setSelectItem() {
                return 50;
            }
        });
        ruler2.setOnSelectItem(new ScaleView.SelectItem() {
            @Override
            public int setSelectItem() {
                return 171;
            }
        });
        /**
         * 监听拖动时值得变化
         */
        ruler1.setOnRulerValueChangeListener(new ScaleView.RulerValue() {
            @Override
            public void value(int i) {
                tvComWeight.setText(i + "kg");
                weight=i;
            }
        });
        ruler2.setOnRulerValueChangeListener(new ScaleView.RulerValue() {
            @Override
            public void value(int i) {
                tvComHeight.setText(i + "cm");
                height=i;
            }
        });

        comGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.com_radio_btn1:
                        sex = "女";
                        break;
                    case R.id.com_radio_btn2:
                        sex = "男";
                        break;
                }
            }
        });


        updateDatePicker.init(1980, 01, 01, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                birth = year + "-" + (monthOfYear + 1) + "-" + dayOfMonth;
            }
        });
    }

    @OnClick({R.id.btn_next, R.id.btn_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_next:

                index++;
                showIndex();


                if (index == 4) {
                    login();

                }
                break;
            case R.id.btn_back:
                index--;
               showIndex();
                break;
        }
    }

    private void showIndex() {
        if (index == 3||index==4) {
            btnNext.setText("完成");
        }else {
            btnNext.setText("下一步");
        }
        if (index < list.size()) {
            textIndex.setText((index + 1) + "/4");

            showView();
        }
    }

    private void login() {

        userBean.userSex = sex;
        userBean.userHeight = height;
        userBean.userWeight = weight;
        userBean.userBirth = birth;
        MyModel.getModel().getService().completeUserInfo(userBean.userId,userBean.userSex,userBean.userHeight,userBean.userWeight,userBean.userBirth).enqueue(new Callback<Li_UserBeanResult>() {
            @Override
            public void onResponse(Call<Li_UserBeanResult> call, Response<Li_UserBeanResult> response) {
                Li_UserBeanResult beanResult = response.body();
                if(beanResult != null && beanResult.getData() != null){
                    //写数据
                    WriteReadSharedPrefs.writeUser(context, beanResult.getData());
                    //读到单例中
                    WriteReadSharedPrefs.readUser(context, userBean);
                    goToMain();
                }else {
                    index=3;
                }
            }

            @Override
            public void onFailure(Call<Li_UserBeanResult> call, Throwable t) {
                index=3;
            }
        });
    }

    public void goToMain() {
        Intent intent = new Intent(CompleteInfoActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    private void showView() {
        if (index == 0) {
            btnBack.setVisibility(View.GONE);
        } else {
            btnBack.setVisibility(View.VISIBLE);
        }
        if (index == 1) {
            tvStyle.setText("体重");
        } else if (index == 2) {
            tvStyle.setText("身高");
        } else if (index == 3) {
            tvStyle.setText("生日");
        }else if (index==0){
            tvStyle.setText("性别");
        }
        for (int i = 0; i < list.size(); i++) {
            View view = list.get(i);
            if (index == i) {
                view.setVisibility(View.VISIBLE);
            } else {
                view.setVisibility(View.GONE);
            }
        }

    }




}
